
package com.mephi.model;


import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import org.apache.log4j.Logger;
import com.mephi.interfaces.Game;
import com.mephi.util.AuxClass;


/**
 * Default model for the JTable used for ChessScoreSheet
 * @author mephisto
 * 2011-07-23
 */
public class GameMovesModel extends DefaultTableModel
{

	/**
	 * Constructs a model from the moves in the game.
	 * 
	 * @param gameModel the gameModel to set
	 * 
	 * @throws NullPointerException if gameModel is null
	 */
	public GameMovesModel(Game<Move> gameModel)
	{
		super(colVec, 0);
		AuxClass.checkArgument(gameModel, 
				new NullPointerException("Unfotunatelly gameModel was not initialized properly! It is null!"));
		this.gameModel = gameModel;
		for (Move it: gameModel.getMovesList())
			addRow(it);
	}
	
	/*@Override
	public int getColumnCount()
	{
		return 3;
	}

	@Override
	public int getRowCount()
	{
		return (gameModel != null) ? gameModel.size() : 0;
	}

	@Override
	public Object getValueAt(int row, int col)
	{
		if (gameModel == null)
        	throw new IllegalStateException("Unfotunatelly gameModel was not initialized properly! It is null!");
		if (col == 0)
			return row+1;
		//JLabel tmpLabel = null;
		if (col == 1)
			return gameModel.get(row).getWhiteMove();
			if (!gameModel.get(row).getWhiteMove().contains("!"))
				return gameModel.get(row).getWhiteMove();
			else
			{
				tmpLabel = new JLabel(gameModel.get(row).getWhiteMove());
				tmpLabel.setBackground(Color.GREEN);
				return tmpLabel;
			}
				
		
		if (col == 2)
			return gameModel.get(row).getBlackMove();
		
		
		return "";
	}*/
	
	/**
	 * @param value the value to set
	 * @param row the row of the table
	 * @param col the column of the table
	 * 
	 * @throws NullPointerException if value is null
	 * @throws IllegalStateException if gameModel is null
	 * @throws IllegalArgumentException if {@code col == 0}
	 */
	@Override
    public void setValueAt(Object value, int row, int col) 
    {
		AuxClass.checkArgument(gameModel, 
				new IllegalStateException("Unfotunatelly gameModel was not initialized properly! It is null!"));
        if (col == 0)
    		throw new IllegalArgumentException("Nothing can be changed in the first column!");  
        String val = (String) value;
        super.setValueAt(val, row, col);
        
        fireTableCellUpdated(row, col);
    }
	
	/**
	 * Adds move only to model of the table, but doesn't add it 
	 * to gameModel
	 * 
	 * @param move the move to add
	 * @throws NullPointerException if the move is null
	 */
	public void addRow(Move move)
	{
		AuxClass.checkArgument(move, new NullPointerException("Move to add cannot be null!"));
		Vector<String> newMove = new Vector<String>(3);
		newMove.add(Integer.toString(getRowCount()+1));
		newMove.add(move.getWhiteMove());
		newMove.add(move.getBlackMove());
		super.addRow(newMove);
	}
	
	/**
	 * Adds move to model of the table and to gameModel.
	 * 
	 * @param move the move to add
	 * @throws NullPointerException if the move is null
	 */
	public void addMove(Move move)
	{
		addRow(move);
		gameModel.add(move);
	}
	
	/**
	 * Removes the row at index from the model of table, doesn't remove it 
	 * from gameModel.
	 * 
	 * @param index the index of move for removing to set
	 * @throws ArrayIndexOutOfBoundsException if the index is invalid
	 */
	@Override
	public void removeRow(int index)
	{
		super.removeRow(index);
	}
	
	/**
	 * Removes all the rows from the table model, but it doesn't
	 * change state of gameModel.
	 */
	public void removeAllRows()
	{
		for(int i = 0, size = getRowCount(); i<size; i++)
			super.removeRow(i);
	}
	
	/**
	 * Removes all the rows from the table model and all moves from the gameModel.
	 */
	public void removeAllMoves()
	{
		removeAllRows();
		gameModel.clear();
	}
	
	/**
	 * 
	 * @return the gameModel
	 */
	public Game<Move> getGameModel()
	{
		return gameModel;
	}

	private static Vector<String> colVec = new Vector<String>(3);
	static
	{
		colVec.add("Nr");
		colVec.add("White");
		colVec.add("Black");
	}
	
	
	private static final long serialVersionUID = 7001450583177970232L;
	private Logger logger = Logger.getLogger("LOG");
	private Game<Move> gameModel;
}
